package com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.base.response.ResponseCode;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.entity.ResistEarthquakesCalculation;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.entity.ResistEarthquakesDE;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.param.DETasksParam;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.param.ResistEarthquakesCalculationParam;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.param.AssessParam;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.service.ResistEarthquakesDEService;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import com.css.fxfzfxqh.util.ResponseCreator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.InputStream;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/05/24
 * @Description: 抗震能力_确定评估
 */
@Slf4j
@RestController
@RequestMapping("/resistEarthquakesDE")
public class ResistEarthquakesDEController {

    @Resource
    private ResistEarthquakesDEService resistEarthquakesDEService;

    /**
     * 新增评估
     *
     * @param param 参数
     * @return
     */
    @PostMapping("/addAssess")
    public RestResponse addAssess(@RequestBody ResistEarthquakesDE param) throws Exception{
        resistEarthquakesDEService.addAssess(param);
        return RestResponse.succeed("新增成功");
    }

    /**
     * 查看
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(String id) {
        RestResponse response = null;
        try {
            ResistEarthquakesDE vehManage=resistEarthquakesDEService.findById(id);
            response = RestResponse.succeed(vehManage);
        } catch (Exception e) {
            String errorMessage = "查看失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 修改评估
     *
     * @param param 参数
     * @return
     */
    @PostMapping("/upDateAssess")
    public RestResponse upDateAssess(@RequestBody ResistEarthquakesDE param) {
        try {
            resistEarthquakesDEService.upDateAssess(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改评估失败!", e);
            return RestResponse.fail("修改失败!");
        }
    }

    /**
     * 获取评估code
     */
    @PostMapping("/getCode")
    public RestResponse getCode(@RequestBody ResistEarthquakesDE param) {
        try {
            String code = resistEarthquakesDEService.getCode(param);
            return RestResponse.succeed(code);
        } catch (Exception e) {
            String errormessage = "生成code失败";
            log.error(errormessage, e);
            return RestResponse.fail("获取失败");
        }

    }

    /**
     * 分页列表
     *
     * @param param 参数
     * @return page
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request, AssessParam param) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = resistEarthquakesDEService.getPage(curPage, pageSize, param);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 计算
     *
     * @param param 参数
     * @return
     */
    @PostMapping("/calculation")
    public RestResponse calculation(@RequestBody DETasksParam param)  {
        try{
            return resistEarthquakesDEService.calculation(param);
        }catch (Exception e){
            return ResponseCreator.createFailResponse(e.getMessage(), ResponseCode.EVALUATE_ERROR);
        }

    }

    /**
     * 导入
     *
     * @param type 计算的类型 1省市县烈度2省市县概率,3坐标烈度,4坐标概率
     * @param id   评估记录id
     * @return
     */
    @PostMapping("/importExcel")
    public RestResponse reportImport(HttpServletRequest req, String type, String id,String code) {
        RestResponse response = null;
        try {
            //获取上传到服务器的临时文件对象
            Part part = req.getPart("file");
            //获取临时文件的输入流
            InputStream inputStream = part.getInputStream();
            String str = resistEarthquakesDEService.reportImport(inputStream, type, id,code);
            response = RestResponse.succeed(str);
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 导出
     *
     * @param param 参数
     */
    @PostMapping("/exportData")
    public void exportData(HttpServletResponse response, ResistEarthquakesCalculationParam param) {
        resistEarthquakesDEService.exportData(response, param);
    }


    /**
     * 查询计算值
     *
     * @param seismicInputMode 震输入方式0烈度,1四个概率水准,2地震影响场
     * @param id               评估记录id
     * @param condition        条件(地震烈度 或 四个概率水准)
     * @param damageDegree     破坏程度
     * @return
     */
    @GetMapping("/queryCalculation")
    public RestResponse queryCalculation(HttpServletRequest request,String seismicInputMode, String id, String condition, String damageDegree) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = resistEarthquakesDEService.queryCalculation(curPage,pageSize,seismicInputMode, id, condition, damageDegree);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改经验评估与修正值
     *
     * @return
     */
    @PostMapping("/updateCorrectionValue")
    public RestResponse updateCorrectionValue(@RequestBody ResistEarthquakesCalculation param) {
        RestResponse restResponse = null;
        try {
            resistEarthquakesDEService.updateCorrectionValue(param);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

}
